package com.bidv.wcm;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.Document;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.RichTextComponent;
import com.ibm.workplace.wcm.api.VirtualPortalScopedAction;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.exceptions.WCMException;
import com.ibm.workplace.wcm.api.query.Query;
import com.ibm.workplace.wcm.api.query.ResultIterator;
import com.ibm.workplace.wcm.api.query.Selectors;

public class VPListSearch implements VirtualPortalScopedAction{

	
	public String Data = "";
	public String Dir = "";
	public int Npp = 0;
	public int PageNum = 0;
	public String cp = "";
	public String context = "";
	public String doclib = "";
	public String index = "";
	public String SearchKeyword = "";
	public StringBuilder sb2 = new StringBuilder();
	public Vector<String> v = new Vector<String>();
	public Vector<String> v2 = new Vector<String>();
	//public constructor
	public VPListSearch(){
	}


	@Override
	public void run() throws WCMException {
		Repository repo = WCM_API.getRepository();
		Workspace myWorkspace = repo.getSystemWorkspace();
		myWorkspace.login();
		DocumentLibrary docLib = myWorkspace.getDocumentLibrary(doclib);
		myWorkspace.setCurrentDocumentLibrary(docLib);
		
	    DocumentId<?> site = myWorkspace.findAllByPath(Dir,
				DocumentTypes.SiteArea,
				Workspace.WORKFLOWSTATUS_PUBLISHED,
				new DocumentLibrary[] { myWorkspace.getDocumentLibrary(doclib) }
				).next();
	    
	    DocumentId<?> newsAT = myWorkspace.findAllByPath("News",
				DocumentTypes.AuthoringTemplate,
				Workspace.WORKFLOWSTATUS_PUBLISHED,
				new DocumentLibrary[] { myWorkspace.getDocumentLibrary("CTC Design") }
				).next();
    
	    DocumentIdIterator<Content> docContent = myWorkspace.contentSearch(newsAT, new DocumentId[] {site}, null, null, false, false, false, Workspace.SORT_KEY_PUBLISH_DATE,null,null,false);

	    

		DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		while (docContent.hasNext()) {
			DocumentId<Content> contentid = docContent.next();


				Content contentc = myWorkspace.getById(contentid);
				String id = contentid.getId();
				String label = contentc.getTitle()+" ("+df.format(contentc.getDateEnteredStage())+")";
				
				StringBuilder sb2 = new StringBuilder();
			    DocumentId<Document> documentId = myWorkspace.createDocumentId(id);
				Query myQuery = myWorkspace.getQueryService().createQuery();
				myQuery.addSelector(Selectors.idEquals(documentId));
				myWorkspace.getQueryService().execute(myQuery);
				ResultIterator<?> iterator =  myWorkspace.getQueryService().execute(myQuery);
				if(iterator.hasNext()) {
					Content content = (Content) iterator.next();
					sb2.append("<h2>"+content.getTitle()+"</h2>");
					RichTextComponent  r1 = (RichTextComponent) content.getComponent("Summary");
					sb2.append(r1.getRichText());
					RichTextComponent  r2 = (RichTextComponent) content.getComponent("Body");
					sb2.append(r2.getRichText());
				}
				sb2.append(contentc.getTitle());
				String textSearch = sb2.toString();
				
				if (textSearch.indexOf(SearchKeyword)>0){
					v.add(label);
					v2.add(id);
				}
		}

	}
	

}